package Frame.battleanimation;

import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import VWorldGameStudio.FCAnimation;
import VWorldGameStudio.FCMonster;

import java.awt.*;

import static FCRendering.FCRMaster.fcv;
import static FCRendering.FCRMaster.monLib;

//呼叫同伴
public class HuJiaoHuoBanAni extends ABattleAni{

    FCAnimation fcAnimation;
    int[] startpoints;

    private int pngIndex = -1;
    private int startFrame = 0;
    private int moveInFrames = 0;
    private int[] firePaintPoints;
    private int[] moveStart = new int[2];
    private int[] moveEnd = new int[2];
    private int[] resultPoint = new int[2];
;    FCMonster srcMs;
    Integer[] endPoit = null;

    public HuJiaoHuoBanAni(FCMonster monster, int[] st){
        srcMs = monster;
        fcAnimation = FCRMaster.animationLib.getAnimationOfID("partnerHelp");
        startpoints = st;
        isPlayerAttackMode = false;
        if(isPlayerAttackMode) {
            playerOffset[0] = (int) (20 * fcv.getProportion());
            startpoints[0] -= playerOffset[0];
        }else {
            playerOffset[0] = (int) (40 * fcv.getProportion());
            startpoints[0] += playerOffset[0];
        }

        if (fcAnimation.getFirePngUrl().size() > 0) {
            startFrame = fcAnimation.fireFrameTime / 16;
            if(fcAnimation.getFirePngUrl().size() > startFrame){
                startFrame = fcAnimation.getFirePngUrl().size();
            }
        }
        endPoit = FCRMaster.fcrMaster.fcMonsterBuildCoor.canAddNewMonster(monster);
        if(endPoit != null) {
            int v = (int) (200 * fcv.getProportion());//速度200像素
            moveInFrames = (int) (16 * (endPoit[0] + endPoit[2]) * fcv.getProportion() / v);
            moveStart[0] = -endPoit[2];
            moveStart[1] = endPoit[1];
            moveEnd[0] = endPoit[0];
            moveEnd[1] = endPoit[1];

            endPoit[0] = moveStart[0];
            endPoit[4] = 0;
            fcv.getBattleMonsterCoor().add(endPoit);
            fcv.getBattleMonster().add(monLib.getMonsterOfID(monster.getName()).getClone());
        }else {
            moveInFrames = 10;//招不来伙伴
        }
        allFrames = startFrame + moveInFrames;
    }

    @Override
    public void nextFrame(int frame) {
        if (frame <= startFrame) {
            int p = startFrame / fcAnimation.getFirePngUrl().size();
            int index = frame / p + 1;
            pngIndex = Math.min(index, fcAnimation.getFirePngUrl().size() - 1);
            if (firePaintPoints == null) {
                firePaintPoints = new int[2];
                firePaintPoints[0] = startpoints[0] + (int) (fcAnimation.firePngCenterOffsetX * fcv.getProportion());
                firePaintPoints[1] = startpoints[1] + (int) (fcAnimation.firePngCenterOffsetY * fcv.getProportion());
            }
        }else if(endPoit != null){
            AnimationUtil.getAnimatePoint(resultPoint, moveStart, moveEnd, (float) frame / allFrames);
            endPoit[0] = resultPoint[0];
        }

//        if (frame == allFrames-2) {
//            if(endPoit != null) {
//                fcv.getMessage().add(
//                        FCRConstants.SYSTEM + FCRConstants.BATTLE + " " + srcMs.getName() + "呼叫同伴成功");
//            }else {
//                fcv.getMessage().add(
//                        FCRConstants.SYSTEM + FCRConstants.BATTLE + " " + srcMs.getName() + "呼叫同伴失败");
//            }
//            fcv.showMessage();
//        }
    }

    @Override
    public void paintBattleSceen(Graphics g, FCRMaster fcrMaster) {
        if(pngIndex != -1){
            g.drawImage(fcAnimation.getFirePng().get(pngIndex), firePaintPoints[0], firePaintPoints[1],
                    (int) (fcAnimation.firePngSizeW * fcv.getProportion()), (int) (fcAnimation.firePngSizeH * fcv.getProportion()), fcrMaster);
        }
    }
}
